// All UI function here.


$(this).ready(function() {
	$("#ProjectsPanel").hover(null, hideProjectsPanel);
	$("#ProjectsPanel").click(showProjectsPanel);
});

function showProjectsPanel() {
	$("#ProjectsPanel #ProjectsPanelContent").fadeIn(200);
	$("#ProjectsPanel").animate({width: "42%"}, 500);
}

function hideProjectsPanel() {
	$("#ProjectsPanel #ProjectsPanelContent").fadeOut(200);
	$("#ProjectsPanel").animate({width: 46}, 500);
}

function toggleProjectDetails(li) {
	li.find(".ProjectDetails").slideToggle(200);
}

function toggleRootTask(li) {
	if (li.find(".RootTaskContent").is(':visible')) {
		li.find(".RootTaskContent").slideUp(200, function (){
			li.find(".TasksList > li").remove();
		});
	}
	else {
		pyForm.load_sub_tasks(li.attr("id"));
		li.find(".RootTaskContent").slideDown(200);
	}

	return false;
}

function toggleRootTaskDetails(li) {
	li.find(".RootTaskContent").slideDown(200);
	li.find(".RootTaskContent .RootTaskDetails").slideToggle(200);
	return false;
}

function toggleTask(li) {
	if (li.find(".TaskContent").is(':visible')) {
		li.find(".TaskContent").slideUp(200, function (){
			li.find(".TasksList > li").remove();
		});
	}
	else {
		pyForm.load_sub_tasks(li.attr("id"));
		li.find(".TaskContent").slideDown(200);
	}

	return false;
}

function toggleTaskDetails(li) {
	li.find(".TaskContent").slideDown(200);
	li.find(".TaskContent .TaskDetails").slideToggle(200);
	return false;
}

function showForm(form) {
	$(form).slideDown(200);
}

function hideForm(form) {
	$(form).slideUp(200);
	return true; //confirm reset
}

function removeNewTaskForm() {
	$("#NewTaskFormWrapper").slideUp(200,  function(){
		$(this).remove();
	});
}

function markProjectAsSelected(li) {
	$(".ActiveProject").removeClass("ActiveProject");
	li.addClass("ActiveProject");
}

function clearFormElements(form) {
	$(form).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
				$(this).val('');
				break;
			case 'checkbox':
			case 'radio':
				this.checked = false;
		}
	});
}

function clearCentralPanel() {
	$("#RootTasksList > li").remove();
}

function activateMilestoneInputForm() {
	hideProjectsPanel();
	showForm("#NewMilestoneFormWrapper");
}

function showNewProjectForm() {
    $("#ButtonCreateNewProject").slideUp(200);
    showForm("#NewProjectFormWrapper");
}

function hideNewProjectForm() {
    hideForm("#NewProjectFormWrapper");
    $("#ButtonCreateNewProject").slideDown(200);
    $("#NewProjectFormWrapper form .CreationFormEditableDiv").html("");
}

function insertNewTaskInputFormAfter(elem, subtask) {
	if ($("#NewTaskFormWrapper").length > 0) {
		removeNewTaskForm();
	}
	var form_template = template_CreateTaskForm(subtask);
	$(form_template).insertAfter(elem).slideDown(200);
}

function insertNewTaskInputFormBefore(elem, subtask, p_id) {
	if ($("#NewTaskFormWrapper").length > 0) {
		removeNewTaskForm();
	}
	var form_template = template_CreateTaskForm(subtask, p_id);
	$(form_template).insertBefore(elem).slideDown(200);
}

//function insertCommentForm(obj) {
//	//Comment form is inserted after the given object obj
//	var form_template = [
//			'<div id="new_comment_form">', //TODO: add this css class
//			'   <div id="new_comment_body" contentEditable="true">', //TODO: this too
//			'   </div>',
//			'   <button style="display: inline;">Delete</button>',
//			'   <button style="display: inline;">Add</button>',
//			'</div>'
//	].join('\n');
//	$(form_template).insertAfter(obj);
//}
